<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm"  :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="姓名" prop="lPillarId" >
            <el-select v-model="queryParams.lPillarId" filterable placeholder="请选择">
            <el-option
              v-for="item in friendsList"
              :key="item.lUserId"
              :label="item.lRealName"
              :value="item.lUserId">
            </el-option>
          </el-select>
          </el-form-item>
        <el-form-item label="事件类型" prop="lEventType">
          <el-select
              v-model="queryParams.lEventType"
              placeholder="请输入事件类型"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in life_event_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
        </el-form-item>
        <el-form-item label="礼尚往来" prop="lGiftWay">
          <el-select
              v-model="queryParams.lGiftWay"
              placeholder="请输入"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in life_gift_way"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
        </el-form-item>
        <el-form-item label="礼金" prop="lCashGift">
          <el-input
            v-model="queryParams.lCashGift"
            placeholder="请输入礼金"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="礼物" prop="lGift">
          <el-input
            v-model="queryParams.lGift"
            placeholder="请输入礼物"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="queryParams.remark"
            placeholder="请输入备注"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-tag id="tagMoney" type="danger">￥{{totalMoney}}</el-tag>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['life:list:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="Edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['life:list:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="Delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['life:list:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['life:list:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getGiftList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="giftList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
  
        <el-table-column prop="lGiftWay" label="礼尚往来" width="150" align="center">
        <template #default="scope">
          <dict-tag :options="life_gift_way" :value="scope.row.lGiftWay"/>
          </template>
        </el-table-column>

        <el-table-column prop="lEventType" label="事件类型" width="150" align="center">
        <template #default="scope">
          <dict-tag :options="life_event_type" :value="scope.row.lEventType"/>
          </template>
        </el-table-column>
        <el-table-column label="姓名" align="center" prop="lFamilyFriends.lRealName" />
        <el-table-column label="礼金" align="center" prop="lCashGift" />
        <el-table-column label="礼物" align="center" prop="lGift" />
        <el-table-column label="备注" align="center" prop="remark" />

        <el-table-column label="创建时间" align="center" prop="createTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
        <el-table-column label="操作"  width="180" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
               <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['life:list:edit']">修改</el-button>
               <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['life:list:remove']">删除</el-button>
            </template>
      </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getGiftList"
      />
  
      <!-- 添加或修改人情世故对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body>
        <el-form ref="listRef" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="礼尚往来">
          <el-select v-model="form.lGiftWay" placeholder="请选择类型">
            <el-option
              v-for="dict in life_gift_way"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="事件类型">
          <el-select v-model="form.lEventType" placeholder="请选择事件类型">
            <el-option
              v-for="dict in life_event_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

          <el-form-item label="姓名" >
            <el-select v-model="form.lPillarId" filterable placeholder="请选择">
            <el-option
              v-for="item in friendsList"
              :key="item.lUserId"
              :label="item.lRealName"
              :value="item.lUserId">
            </el-option>
          </el-select>
          </el-form-item>
      
          <el-form-item label="礼金" prop="lCashGift">
            <el-input v-model="form.lCashGift" placeholder="请输入礼金" />
          </el-form-item>
          <el-form-item label="礼物" prop="lGift">
            <el-input v-model="form.lGift" type="textarea" placeholder="请输入礼物" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="submitForm">确 定</el-button>
               <el-button @click="cancel">取 消</el-button>
            </div>
       </template>
      </el-dialog>
    </div>
  </template>
  
<script setup name="List">
  import { listList, getList, delList, addList, updateList } from "@/api/life/gift";
  import { listFriends } from "@/api/life/friends";

  const { proxy } = getCurrentInstance();
  const { life_gift_way,life_event_type} = proxy.useDict('life_gift_way','life_event_type');


const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const showSearch = ref(true);
const giftList = ref([]);
const total = ref(0)
const friendTotal = ref(0)
const friendsList = ref([]);
const title = ref("");
const open = ref(false);
const floading = ref(true)
const totalMoney = ref(0.00)


const data = reactive({
  form: {},
  queryParams: {
          pageNum: 1,
          pageSize: 10,
          lEventType: null,
          lPillarId: null,
          lGiftWay: null,
          lCashGift: null,
          lGift: null,
          lGiftDescription: null,
        },
  userParams: {
      pageNum: 1,
      pageSize: 10000,
      lRealName: null,
  },
        // 表单校验
        rules: {
          lEventType: [
            { required: true, message: "事件类型不能为空", trigger: "blur" }
          ],
          lPillarId: [
            { required: true, message: "主场不能为空", trigger: "blur" }
          ],
          lGiftWay: [
            { required: true, message: "礼物方式不能为空", trigger: "blur" }
          ],
          
        },
});
const { queryParams,userParams, form, rules } = toRefs(data);

/** 查询亲戚朋友列表 */
function  getUserList() {
    listFriends(userParams.value).then(response => {
      friendsList.value = response.rows;
      friendTotal.value = response.total;
    });
  }

/** 查询人情世故列表 */
function   getGiftList() {
  loading.value = true;
  listList(queryParams.value).then(response => {
    giftList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    if(giftList.value.length==0){
      totalMoney.value = 0.00
    }else{
      totalMoney.value = giftList.value[0].totalMoney;
    }
  });
}
// 取消按钮
function  cancel() {
  open.value = false;
  reset();
}
// 表单重置
function  reset() {
  form.value = {
    lGiftId: null,
    lEventType: null,
    lPillarId: null,
    lGiftWay: null,
    lCashGift: null,
    lGift: null,
    lGiftDescription: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
  };
  proxy.resetForm("listRef");
}
/** 搜索按钮操作 */
function  handleQuery() {
  queryParams.value.pageNum = 1;
  getGiftList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryForm");
  handleQuery();
}
// 多选框选中数据
function  handleSelectionChange(selection) {
  ids.value = selection.map(item => item.lGiftId)
  single.value = selection.length!==1
  multiple.value = !selection.length
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加人情世故";
}
/** 修改按钮操作 */
function  handleUpdate(row) {
  reset();
  const lGiftId = row.lGiftId || ids.value
  getList(lGiftId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改人情世故";
  });
}
/** 提交按钮 */
function  submitForm() {
  proxy.$refs["listRef"].validate(valid => {
  if (valid) {
    if (form.value.lGiftId != null) {
      updateList(form.value).then(response => {
        proxy.$modal.msgSuccess("修改成功");
        open.value = false;
        getGiftList();
      });
    } else {
      addList(form.value).then(response => {
        proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getGiftList();
      });
    }
  }
});
}
/** 删除按钮操作 */
function   handleDelete(row) {
  const lGiftIds = row.lGiftId || this.ids.value;
  proxy.$modal.confirm('是否确认删除人情世故编号为"' + lGiftIds + '"的数据项？').then(function() {
    return delList(lGiftIds);
  }).then(() => {
    getGiftList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}
/** 导出按钮操作 */
function   handleExport() {
    proxy.download('life/list/export', {
      ...queryParams.value
    }, `list_${new Date().getTime()}.xlsx`)
  }
getGiftList();
getUserList();

</script>
<style>
#tagMoney{
  margin-left: 20px;
  font-size: large;
}

</style>